package com.aigou.admin.service.impl;

import com.aigou.admin.dao.ProductDao;
import com.aigou.admin.dao.impl.ProductDaoImpl;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Product;
import com.aigou.admin.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/11/29 15:34
 * @Version: 1.0
 */
@Service
@AllArgsConstructor
public class ProductServiceImpl implements ProductService {

    private ProductDao productDao;

    @Override
    public List<Product> findAll() {
        return productDao.selectAll();
    }

    @Override
    public Product findById(String id) {
        return productDao.selectById(id);
    }

    @Override
    public List<Product> findByName(String product_name) {
        return productDao.selectByName(product_name);
    }

    @Override
    public Boolean save(Product product) {
        return productDao.insert(product);
    }

    @Override
    public Boolean update(Product product) {
        return productDao.update(product);
    }

    @Override
    public Boolean remove(String id) {
        return productDao.delete(id);
    }

    @Override
    public PageEntity<Product> findProductByPage(Integer currentPage, Integer pageSize) {
        Integer total = productDao.selectProductCount();
        List<Product> products = productDao.selectProductByLimit((currentPage - 1) * pageSize, pageSize);
        PageEntity<Product> pageEntity = new PageEntity<>();
        pageEntity.setTotal(total);
        pageEntity.setPageSize(pageSize);
        pageEntity.setCurrent(currentPage);
        pageEntity.setRecords(products);
        return pageEntity;
    }
}
